<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Home Class
 *
 * @package XGO CMS v2.0
 * @subpackage News
 * @author Manh Nguyen - 30/07/2012
 * @link http://sunsoft.vn
 */
class Cart extends CI_Controller {

    private $client_ip;

    /**
     * Contrust class
     *
     * @access public
     */
    public function __construct() {
        parent::__construct();

        $this->load->database();
        $this->load->library('datamapper');
        $this->load->library('smarty3', '', 'smarty');
        $this->load->library('session');
        
        $this->load->model('cart_model');
        $this->smarty = new CI_Smarty3();
        
        require_once APPPATH.'modules/frontend/common/main_menu.php';
        require_once APPPATH.'modules/frontend/common/top.php';
        require_once APPPATH.'modules/frontend/common/footer.php';
    }

    public function index() {
        //echo $this->current_url;
        //exit;
        $cart = $this->cart->contents();

        if($this->session->userdata('user_info'))
        {
            $userInfo = $this->session->userdata('user_info');
            $this->smarty->assign('userinfo', $userInfo);
        }
        
        $this->session->set_flashdata('redirect', $this->uri->uri_string());
        $this->smarty->assign(array('cart'=>$cart,'news_model'=>new News_model()));
        $this->smarty->view('frontend/account/02_cart','cart');
        $this->smarty->display('template/frontend/account');
    }
    
    function add_cart_item(){
		
        //$itemID = intval($this->input->post('proid'));
        
        if($this->cart_model->validate_add_cart_item() == TRUE){

                // Check if user has javascript enabled
                if($this->input->post('ajax') != '1'){
                        redirect('cart'); // If javascript is not enabled, reload the page with new data
                }else{
                        echo json_encode(array('result'=>'true','total'=>$this->cart->total_items())); // If javascript is enabled, return true, so the cart gets updated
                }
        }
    }
    
    function update_cart() {
        $this->cart_model->validate_update_cart();

        $this->smarty->assign(array('countCart'=>$this->cart->total_items(),'cart' => $this->cart->contents(), 'news_model' => new News_model()));
        $this->smarty->display('frontend/account/02_table_cart');
        //redirect('cart');
    }
    
    function delete_item_in_cart() {
        $rowid = $this->input->post('rowid');
        $this->cart_model->validate_delete_item();
        $this->smarty->assign(array('countCart'=>$this->cart->total_items(),'cart' => $this->cart->contents(), 'news_model' => new News_model()));
        
        //$this->smarty->view('frontend/account/02_cart');
        $this->smarty->display('frontend/account/02_table_cart');

    }


     public function ip_address() {
        echo '<center><h1>Your IP Address Is: ' . $this->input->ip_address() . '</h1></center>';
    }

    public function cpanel() {
        redirect(site_url('auth/cpanel/login'));
    }

}
/* End of file auth/cpanel.php */
/* Location: ./system/application/controllers/auth/cpanel.php */